package com.yrot.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yrot.admin.entity.SysLogEntity;
import com.yrot.admin.service.ISysLogService;
import com.yrot.common.result.Result;
import com.yrot.common.result.ResultUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hemin
 * @since 2019-04-02
 */
@RestController
@RequestMapping("/admin/log")
public class SysLogController extends BaseController{

    @Autowired
    ISysLogService logService;

    @GetMapping("/logs")
    public Result questionBanks(int page, int limit,
                                @RequestParam(required = false) String username,
                                @RequestParam(required = false) String name){
        QueryWrapper<SysLogEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("log_id");
        if(StringUtils.isNotEmpty(username)){
            queryWrapper.like("username",username);
        }
        if(StringUtils.isNotEmpty(name)){
            queryWrapper.like("name",name);
        }
        Page<SysLogEntity> pages = new Page<>(page, limit);
        IPage<SysLogEntity> logPages = logService.page(pages, queryWrapper);
        return ResultUtils.success(logPages.getTotal(),logPages.getRecords());
    }

    @GetMapping("/myLogs")
    public Result Logs(int page, int limit){
        QueryWrapper<SysLogEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("log_id");
        queryWrapper.eq("username",sysExpert().getExpertUsername());
        Page<SysLogEntity> pages = new Page<>(page, limit);
        IPage<SysLogEntity> logPages = logService.page(pages, queryWrapper);
        return ResultUtils.success(logPages.getTotal(),logPages.getRecords());
    }
}
